import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-04-13
 * Time: 10:17
 */
public class TcpEcoClient {
Socket socket = null;
    public TcpEcoClient(String ip,int port) throws IOException {
        socket = new Socket(ip,port);
    }
    public void  start() {
        Scanner scanner = new Scanner(System.in);
     try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()

             ){
         PrintWriter printWriter = new PrintWriter(outputStream);
         Scanner  scannerFromSocket = new Scanner(inputStream);
         while (true){
             System.out.println("->");
             String request = scanner.next();
             printWriter.println(request);
             printWriter.flush();
             String response =scannerFromSocket.next();
             System.out.println("请求为"+request+"响应为"+response);
         }

     } catch (IOException e) {
         e.printStackTrace();
     }
    }
    public static void main(String[] args) throws IOException {
        TcpEcoClient tcpEcoClient = new TcpEcoClient("127.0.0.1", 9000);
        tcpEcoClient.start();
    }
}
